
const path = require('path');
const wallpaperControllers = require('../services/wallpaper');
const { weatherApi, wallpaperApi } = require('../api/index');
module.exports = {
    async getData (ctx) {
        let data = await wallpaperControllers.getList(ctx)
        let res = await wallpaperApi.wallpaper(ctx.query)
        let list = res.body ? res.body.map(el => {
            return {
                category: el.user.name,
                class_id: el.user.id,
                id: el.id,
                live_open: el.liked_by_user,
                tag: el.alt_description,
                url: el.urls.regular
            }
        }) : []
        ctx.body = {
            success: true,
            data: {
                ...data,
                count: list.length,
                total_count: list.length * 1000,
                currentPage: ctx.query.currentPage || 1,
                pageSize: ctx.query.pageSize || 20,
                list
            },
            message: '操作成功',
            code: 200
        };
    },
    async render (ctx) {
        let data = await wallpaperControllers.getList(ctx)
        let params = {}
        params.location = ctx.query.location || '深圳'
        let weather = await weatherApi.getWeatherNow(params)
        let pageData = {
            data,
            weather: weather.results,
            query: ctx.query || {}
        }
        await ctx.render({
            path: path.resolve(__dirname, '../views/pages/wallpaper.vue'),
            components: {
                Layout: path.resolve(__dirname, '../views/layout/index.vue'),
                Header: {
                    path: path.resolve(__dirname, '../views/layout/header.vue'),
                    data () {
                        return {
                            ...pageData
                        }
                    },
                },
                Tabs: {
                    path: path.resolve(__dirname, '../views/layout/tabs.vue'),
                    data () {
                        return {
                            ...pageData
                        }
                    },
                },
            },
            data () {
                return {
                    ...pageData
                }
            },
            created () {
            },
            methods: {
            }
        })
    }
}